package com.vero.inventory.service.impl;

import java.util.List;
import java.util.concurrent.TimeUnit;

import com.vero.common.core.utils.DateUtils;
import com.vero.inventory.service.IVaccineService;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.vero.inventory.mapper.VaccineMapper;
import com.vero.system.api.domain.Vaccine;

/**
 * 疫苗信息Service业务层处理
 *
 * @author ruoyi
 * @date 2025-03-06
 */
@Service
public class VaccineServiceImpl implements IVaccineService {
    @Autowired
    private VaccineMapper vaccineMapper;

    /**
     * 查询疫苗信息
     *
     * @param vaccineId 疫苗信息主键
     * @return 疫苗信息
     */
    @Override
    public Vaccine selectVaccineByVaccineId(Long vaccineId) {
        return vaccineMapper.selectVaccineByVaccineId(vaccineId);
    }

    /**
     * 查询疫苗信息列表
     *
     * @param vaccine 疫苗信息
     * @return 疫苗信息
     */
    @Override
    public List<Vaccine> selectVaccineList(Vaccine vaccine) {
        return vaccineMapper.selectVaccineList(vaccine);
    }

    /**
     * 新增疫苗信息
     *
     * @param vaccine 疫苗信息
     * @return 结果
     */
    @Override
    public int insertVaccine(Vaccine vaccine) {
        return vaccineMapper.insertVaccine(vaccine);
    }

    /**
     * 修改疫苗信息
     *
     * @param vaccine 疫苗信息
     * @return 结果
     */
    @Override
    public int updateVaccine(Vaccine vaccine) {
        vaccine.setUpdateTime(DateUtils.getNowDate());
        return vaccineMapper.updateVaccine(vaccine);
    }

    /**
     * 批量删除疫苗信息
     *
     * @param vaccineIds 需要删除的疫苗信息主键
     * @return 结果
     */
    @Override
    public int deleteVaccineByVaccineIds(Long[] vaccineIds) {
        return vaccineMapper.deleteVaccineByVaccineIds(vaccineIds);
    }

    /**
     * 删除疫苗信息信息
     *
     * @param vaccineId 疫苗信息主键
     * @return 结果
     */
    @Override
    public int deleteVaccineByVaccineId(Long vaccineId) {
        return vaccineMapper.deleteVaccineByVaccineId(vaccineId);
    }

    @Autowired
    private RedissonClient redissonClient;

    private static final String LOCK_PREFIX = "lock:vaccine:";

    @Override
    public int updateVaccineStock(Vaccine vaccine) {

        String lockKey = LOCK_PREFIX + vaccine.getVaccineId();
        RLock lock = redissonClient.getLock(lockKey);

        try {
            if (lock.tryLock(5, 10, TimeUnit.SECONDS)) {
                try {
                    // 查询当前库存
                    Vaccine dbVaccine = vaccineMapper.selectVaccineByVaccineId(vaccine.getVaccineId());
                    if (dbVaccine == null) {
                        throw new RuntimeException("疫苗不存在");
                    }

                    Long newQuantity = dbVaccine.getQuantity() + vaccine.getQuantity();
                    if (newQuantity < 0) {
                        throw new RuntimeException("库存不足，无法减少");
                    }
                    dbVaccine.setQuantity(newQuantity);
                    // 更新库存
                    return vaccineMapper.updateVaccine(dbVaccine);
                } finally {
                    if (lock.isHeldByCurrentThread()) {
                        lock.unlock();
                    }
                }
            } else {
                throw new RuntimeException("系统繁忙，请稍后重试");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("获取锁失败", e);
        }
    }
}

